﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace TPCN.DAL
{
   public class DanhMucHangDAL
    {
        public static DanhMucHangObj GetByID(string DanhmucHangID)
        {
            DanhMucHangObj obj = new DanhMucHangObj();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "sp_tbDanhMucHang_GetByID";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@DanhmucHangID", DanhmucHangID);
                using (SqlDataReader dr = Database.db.GetDataToDataReader(cmd))
                {
                    if (dr.HasRows)
                    {
                        dr.Read();
                        obj = obj.SetValuesFromDataReader(dr);
                    }
                    dr.Close();
                }
            }
            return obj;
        }

        public static List<DanhMucHangObj> GetByAll()
        {
            List<DanhMucHangObj> result = new List<DanhMucHangObj>();
            DanhMucHangObj obj = new DanhMucHangObj();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "sp_tbDanhMucHang_GetByAll";
                cmd.CommandType = CommandType.StoredProcedure;
                using (SqlDataReader dr = Database.db.GetDataToDataReader(cmd))
                {
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            result.Add(obj.SetValuesFromDataReader(dr));
                        }
                    }
                    dr.Close();
                    obj = null;
                }
            }
            return result;
        }

        public static List<DanhMucHangObj> GetByDanhmucID(string DanhmucID)
        {
            List<DanhMucHangObj> result = new List<DanhMucHangObj>();
            DanhMucHangObj obj = new DanhMucHangObj();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "sp_tbDanhMucHang_GetByDanhmucID";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@DanhmucID", DanhmucID);
                using (SqlDataReader dr = Database.db.GetDataToDataReader(cmd))
                {
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            result.Add(obj.SetValuesFromDataReader(dr));
                        }
                    }
                    dr.Close();
                    obj = null;
                }
            }
            return result;
        }

        public static List<DanhMucHangObj> GetByHangID(string HangID)
        {
            List<DanhMucHangObj> result = new List<DanhMucHangObj>();
            DanhMucHangObj obj = new DanhMucHangObj();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "sp_tbDanhmucHang_GetByHangID";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@HangID", HangID);
                using (SqlDataReader dr = Database.db.GetDataToDataReader(cmd))
                {
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            result.Add(obj.SetValuesFromDataReader(dr));
                        }
                    }
                    dr.Close();
                    obj = null;
                }
            }
            return result;
        }

        public static bool Insert(DanhMucHangObj obj)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "sp_tbDanhMucHang_Insert";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@HangID", (obj.HangID == null) ? String.Empty : obj.HangID);
                    cmd.Parameters.AddWithValue("@DanhmucID", (obj.DanhmucID == null) ? String.Empty : obj.DanhmucID);
                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static bool Update(DanhMucHangObj obj)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "sp_tbDanhMucHang_Update";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@DanhmucHangID", (obj.DanhmucHangID == null) ? String.Empty : obj.DanhmucHangID);
                    cmd.Parameters.AddWithValue("@HangID", (obj.HangID == null) ? String.Empty : obj.HangID);
                    cmd.Parameters.AddWithValue("@DanhmucID", (obj.DanhmucID == null) ? String.Empty : obj.DanhmucID);
                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static bool Delete(string DanhmucHangID)
        {
            try
            { 
                using(SqlCommand cmd =new SqlCommand())
                {
                    cmd.CommandText="sp_tbDanhMucHang_DeleteByID";
                    cmd.CommandType=CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@DanhmucHangID",DanhmucHangID);
                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch(Exception)
            {
                return false;
            }
        }

        public static bool DeleteByDanhmucID(string DanhmucID)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "sp_tbDanhMucHang_DeleteByDanhmucID";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@DanhmucID", DanhmucID);
                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool DeleteByHangID(string HangID)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "sp_tbDanhMucHang_DeleteByHangID";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@HangID", HangID);
                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
